

package com.kimo.shop.admin.controller;

import cn.hutool.core.date.DateUtil;
import com.kimo.shop.bean.param.FlowUserAnalysisParam;
import com.kimo.shop.service.FlowUserAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Objects;

/**
 * @author kimo-taozhouchuan on 2018/11/26.
 */
@RestController
@RequestMapping("/admin/flowUserAnalysis")
public class FlowUserAnalysisController {

    @Autowired
    private FlowUserAnalysisService flowUserAnalysisService;


    /**
     * 获取会员分析数据
     * @param flowUserAnalysisParam
     * @return
     */
    @GetMapping("/getUserAnalysisData")
    public ResponseEntity<FlowUserAnalysisParam> getUserAnalysisData(FlowUserAnalysisParam flowUserAnalysisParam) {

        Integer type = flowUserAnalysisParam.getType();
        if (type != 3){
            int day = 0;
            if (type == 1){
                day = -7;
            }else {
                day = -30;
            }
            Date endTime = DateUtil.beginOfDay(new Date());
            flowUserAnalysisParam.setEndTime(endTime);
            flowUserAnalysisParam.setStartTime(DateUtil.offsetDay(endTime,day));
            flowUserAnalysisParam.setStart(flowUserAnalysisParam.getStartTime().getTime());
            flowUserAnalysisParam.setEnd(DateUtil.offsetDay(endTime,-1).getTime());
        }else {
            Date dateEnd = new Date();
            Date startDate = new Date();
            if(Objects.nonNull(flowUserAnalysisParam.getEnd())){
                dateEnd = new Date(flowUserAnalysisParam.getEnd());
            }
            if(Objects.nonNull(flowUserAnalysisParam.getStart())){
                startDate = new Date(flowUserAnalysisParam.getStart());
            }
            flowUserAnalysisParam.setEndTime(dateEnd);
            flowUserAnalysisParam.setStartTime(startDate);
        }
        flowUserAnalysisService.getUserAnalysisData(flowUserAnalysisParam);
        return ResponseEntity.ok(flowUserAnalysisParam);
    }
}
